package com.kelp.codetoolkit.ui.form

import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.JBColor
import com.kelp.codetoolkit.data.ControllerRequestData
import com.kelp.codetoolkit.enums.RemoteInvocationFrameworkEnum
import com.kelp.codetoolkit.respository.CodeToolkitGlobalSettingRepository
import java.awt.FlowLayout
import javax.swing.*
import javax.swing.border.LineBorder

/**
 * @author: liuyuebai
 * @date: 2025/1/25 16:01
 * @description:
 */
class ControllerMethodSettingPanel(project: Project): JPanel() {

    private val controllerRequestData: ControllerRequestData = ControllerRequestData()
    private val controllerPanel: JPanel = JPanel(FlowLayout(FlowLayout.LEFT,5,5))
    private val controllerLabel: JLabel = JLabel("Request Method: ")
    private var requestMethodList  = mutableListOf("Select Request Method","GET", "POST", "DELETE","PUT")
    private val requestMethodModel: DefaultComboBoxModel<String> = DefaultComboBoxModel(requestMethodList.toTypedArray())
    private val requestMethodBox: ComboBox<String> = ComboBox(requestMethodModel)
    private val requestPathLabel = JLabel("Request Path: ")
    private val requestPath: JTextField = JTextField(15)
    private var requestMethodText:String = ""

    init {
        controllerPanel.isVisible = false
        val state = CodeToolkitGlobalSettingRepository.getInstance(project).state

        // 为requestPBoxOne添加监听器，根据其值控制requestPBoxKeyOne的显示
        requestMethodBox.addActionListener {
            requestMethodText = requestMethodBox.selectedItem as String
        }
        if (state.getRemoteInvocationFramework() == RemoteInvocationFrameworkEnum.OPENFEIGN.value){
            controllerPanel.isVisible = true
        }
    }

    fun getControllerPanel(): JPanel {
        controllerPanel.add(controllerLabel)
        controllerPanel.add(Box.createHorizontalStrut(2)) // 添加空白间隔以改善对齐
        controllerPanel.add(requestMethodBox)
        controllerPanel.add(Box.createHorizontalStrut(10)) // 添加空白间隔以改善对齐
        controllerPanel.add(requestPathLabel)
        controllerPanel.add(Box.createHorizontalStrut(2)) // 添加空白间隔以改善对齐
        controllerPanel.add(requestPath)
        // 定义浅灰色
        val lightGray = JBColor.GRAY
        // 创建宽度为 1 像素、颜色为浅灰色的 LineBorder
        val lineBorder = LineBorder(lightGray, 1)
        // 创建带有标题的边框，使用上面创建的 LineBorder 作为边框
        val titledBorder = BorderFactory.createTitledBorder(lineBorder, "Controller Setting")
        controllerPanel.setBorder(titledBorder)
        return controllerPanel
    }

    fun getControllerRequest():ControllerRequestData{
        controllerRequestData.requestMethod = requestMethodText
        controllerRequestData.requestPath = requestPath.text
        return controllerRequestData
    }
}